//******************************************************************************
//
// ATM系统 -  Account.java 
// 参考了 http://www.cs.gordon.edu/courses/cs211/ATMExample/index.html
// 
//******************************************************************************

package banking;

import java.util.List;

import com.mysql.jdbc.PreparedStatement;


/**
* 一个Account对象代表了现实世界里的一个银行账户.
* @author  何希
* @version 10/06/2018
*/
/*
* @author 莫东林
* @version 2018/11/13
*/
public class Account extends BaseOperation{
	// 账号
	String card_no;
	// 密码
	String pwd;
	// 余额
	double balance;
	
	//交易记录
	List<Trade> tradeList;
	
	public Account(String card_no, String pwd, Double balance) {
		this.card_no = card_no;
		this.pwd = pwd;
		this.balance = balance;
	}
	
	/**
	 * 查询卡号是否有效
	 */
	public static boolean checkCardNo(String card_no) throws Exception{
		sql = "select * from account where card_no = ?";
		stmt = (PreparedStatement) handler.getStmt(sql);
		stmt.setString(1, card_no);
		rs = stmt.executeQuery();
		while(rs.next()){
			return true;
		}
		return false;
	}
	
	/**
	 * 通过用户名，密码获取一个银行账户对象
	 * 实际中应该查询数据库。在当前的实现中，我们只有一个账户。
	 * @param card_no
	 * @param pwd
	 * @return
	 * @throws Exception 
	 */
	public static Account getAccount(String card_no, String pwd) throws Exception {
		sql = "select * from account where card_no = ?";
		stmt = (PreparedStatement) handler.getStmt(sql);//根据sql获取stmt
		stmt.setString(1, card_no);//设值查询
		rs = stmt.executeQuery();
		String password = "";//正确密码
		Double balance = 0.00;
		while(rs.next()){
			password = rs.getString("password");
			balance = rs.getDouble("balance");
			if(pwd.equals(password)){
				return  new Account(card_no, password, balance);
			}
		}
		return null;
	}
	/**
	 * 扣款
	 * @param amount
	 * @return 0:成功 1:余额不足 2:失败，服务错误
	 * @throws Exception 
	 */
	public int withdraw(double amount) {
		if(this.balance >= amount) {
			this.balance = this.balance - amount;
			sql = "update account set balance = ? where card_no = ?";
			try {
				stmt = (PreparedStatement) handler.getStmt(sql);
				stmt.setDouble(1, this.balance);
				stmt.setString(2, this.card_no);
				stmt.execute();
				return 0;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 2;
			}
		}else{
			return 1;
		}
	}
	
	/**
	 * 获取账户余额
	 * @return
	 */
	public double getBalance() {
		return this.balance;
	}
	
	
	public String getCard_no() {
		return card_no;
	}

	/**
	 * 存款
	 * @param amount
	 * 0:成功 1:失败
	 */
	public int deposit(double amount) {
		this.balance = this.balance + amount;
		sql = "update account set balance = ? where card_no = ?";
		try {
			stmt = (PreparedStatement) handler.getStmt(sql);
			stmt.setDouble(1, this.balance);
			stmt.setString(2, this.card_no);
			stmt.execute();
			return 0;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 1;
		}
	}
	
	/**
	 * 转账
	 * @param amount 交易金额
	 * @param card_no	乙方卡号
	 * @return 0成功 1余额不足 2失败
	 */
	public int transMoney(double amount, String card_no) {
		int temp = withdraw(amount);
		//余额充足
		if(temp == 0){
			//查询乙方账户余额
			Double money = getAmount(card_no);
			sql = "update account set balance = ? where card_no = ?";
			try {
				stmt = (PreparedStatement) handler.getStmt(sql);
				stmt.setDouble(1, amount + money);
				stmt.setString(2, card_no);
				stmt.execute();
				return 0;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 2;
			}
		}
		return temp;
		
	}
	
	/**
	 * 查询指定账户余额
	 */
	public Double getAmount(String card_no){
		sql = "select balance from account where card_no = ?";
		try {
			stmt = (PreparedStatement) handler.getStmt(sql);
			stmt.setString(1, card_no);
			rs = stmt.executeQuery();
			while(rs.next()){
				return (double) rs.getInt("balance");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
